// SecureLinkedImage.java
package fun.sssdnsy.media.image;

import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.ByteArrayInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class SecureLinkedImage implements LinkedImage {
    private final String secureUri;

    public SecureLinkedImage(String uri) {
        this.secureUri = uri;
    }

    @Override
    public boolean isReal() {
        return true;
    }

    // 返回安全URI格式
    @Override
    public String getImagePath() {
        return secureUri;
    }

    @Override
    public Node createNode() {
        try {
            // 解密过程（示例使用简单反混淆）
            String filename = secureUri.replace("secureimg://", "");
            Path path = Paths.get(ImageSerializer.getImageStoragePath(), filename);
            byte[] data = Files.readAllBytes(path);

            // 反混淆处理
            for (int i = 0; i < data.length; i++) {
                data[i] = (byte) (~data[i]);
            }

            return new ImageView(new Image(new ByteArrayInputStream(data)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
